package com.bianxianmao.find.adapter;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bianxianmao.R;
import com.bianxianmao.find.AllReturnBean;
import com.bumptech.glide.Glide;

import java.util.List;

/**
 * Created by Administrator on 2017/8/7.
 */

public class GridViewAdapter extends BaseAdapter {
    private List<AllReturnBean.ReturnValueBean> list;
    private Context mContext;

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        ViewHolder viewHolder;
        if (view == null) {//为空将布局文件转换成view
            view = View.inflate(mContext, R.layout.recyclerview_item2, null);

            viewHolder = new ViewHolder(view);

            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }

        //设置数据

        String image1 = list.get(position).getImage1();
        Glide.with(mContext).load(image1).into(viewHolder.ivGoods);
        Log.i("aaaa", "getView: " + list);
        viewHolder.tvGoodsinfo.setText(list.get(position).getProductName());
        float dbSurplusCount = (float) (list.get(position).getDbSurplusCount());
        float dbTotalCount = (float) (list.get(position).getDbTotalCount());
        float a = (dbSurplusCount / dbTotalCount) * 100;
        viewHolder.pb.setProgress((int) a);
        viewHolder.tvSurplus.setText(list.get(position).getDbSurplusCount() + "");
        viewHolder.tvAll.setText(list.get(position).getDbTotalCount() + "");

        return view;
    }
}

class ViewHolder {
    public ImageView ivImg;
    public ImageView ivGoods;
    public TextView tvGoodsinfo;
    public RelativeLayout rlGood;
    public ProgressBar pb;
    public TextView tvAll;
    public TextView tvSurplus;

    public ViewHolder(View itemView) {
        ivImg = (ImageView) itemView.findViewById(R.id.iv_img);
        ivGoods = (ImageView) itemView.findViewById(R.id.iv_goods);
        tvGoodsinfo = (TextView) itemView.findViewById(R.id.tv_goodsinfo);
        rlGood = (RelativeLayout) itemView.findViewById(R.id.rl_good);
        pb = (ProgressBar) itemView.findViewById(R.id.pb);
        tvAll = (TextView) itemView.findViewById(R.id.tv_all);
        tvSurplus = (TextView) itemView.findViewById(R.id.tv_surplus);

    }
}
